package org.softmed.rest.editor.xml;

import org.softmed.rest.editor.comps.browser.PagedList;
import org.softmed.rest.editor.config.Application;
import org.softmed.rest.editor.config.Handler;
import org.softmed.rest.editor.config.HttpMethod;
import org.softmed.rest.editor.config.MimeTypeHandler;
import org.softmed.rest.editor.config.Module;
import org.softmed.rest.editor.config.Parameter;
import org.softmed.rest.editor.config.Resource;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XStreamConfigurator {

	public static XStream getXStream() {
		XStream streamer = new XStream(new DomDriver("UTF-8"));
		configurePagedListComplete(streamer);
		return streamer;
	}

	public static void configurePagedListComplete(XStream streamer) {
		configurePagedList(streamer);
		configureStreamerApplication(streamer);
		configureStreamerModule(streamer);
		configureStreamerResource(streamer);
		configureStreamerHttpMethod(streamer);
		configureStreamerMimeTypeHandler(streamer);
		configureStreamerHandler(streamer);
		configureStreamerParameter(streamer);

	}

	public static void configurePagedList(XStream streamer) {

		streamer.alias("paged-list", PagedList.class);
		streamer.addImplicitCollection(PagedList.class, "items");

		streamer.useAttributeFor(PagedList.class, "currentPageNumber");
		streamer.useAttributeFor(PagedList.class, "itemsPerPage");
		streamer.useAttributeFor(PagedList.class, "itemCount");

		streamer.useAttributeFor(PagedList.class, "previousURI");
		streamer.useAttributeFor(PagedList.class, "nextURI");
		streamer.useAttributeFor(PagedList.class, "firstURI");
		streamer.useAttributeFor(PagedList.class, "lastURI");

		streamer.aliasAttribute(PagedList.class, "currentPageNumber", "page");
		streamer.aliasAttribute(PagedList.class, "itemsPerPage",
				"items-per-page");
		streamer.aliasAttribute(PagedList.class, "itemCount", "item-count");

		streamer.aliasAttribute(PagedList.class, "previousURI", "previous");
		streamer.aliasAttribute(PagedList.class, "nextURI", "next");
		streamer.aliasAttribute(PagedList.class, "firstURI", "first");
		streamer.aliasAttribute(PagedList.class, "lastURI", "last");

	}

	public static void configureStreamerApplication(XStream streamer) {
		streamer.alias("application", Application.class);
		streamer.aliasField("relative-uri", Application.class, "relativeUri");
		streamer.useAttributeFor(Application.class, "id");
		streamer.useAttributeFor(Application.class, "active");
		streamer.useAttributeFor(Application.class, "uri");
		streamer.useAttributeFor(Application.class, "name");
		streamer.omitField(Application.class, "modules");

	}

	public static void configureStreamerHandler(XStream streamer) {
		streamer.alias("handler", Handler.class);
		streamer.useAttributeFor("id", String.class);
		streamer.useAttributeFor("uri", String.class);
		streamer.useAttributeFor("active", boolean.class);
		streamer.useAttributeFor(Handler.class, "id");
		streamer.useAttributeFor(Handler.class, "uri");
		streamer.useAttributeFor(Handler.class, "active");
		streamer.omitField(Handler.class, "parameters");

	}

	public static void configureStreamerHttpMethod(XStream streamer) {
		streamer.alias("http-method", HttpMethod.class);
		streamer.aliasField("relative-uri", HttpMethod.class, "relativeUri");
		streamer.useAttributeFor(HttpMethod.class, "id");
		streamer.useAttributeFor(HttpMethod.class, "active");
		streamer.useAttributeFor(HttpMethod.class, "uri");
		streamer.useAttributeFor(HttpMethod.class, "name");
		streamer.omitField(HttpMethod.class, "mime-type-handlers");
		// streamer.omitField(HttpMethod.class, "handler");

	}

	public static void configureStreamerMimeTypeHandler(XStream streamer) {
		streamer.alias("mime-type-handler", MimeTypeHandler.class);
		streamer.useAttributeFor(MimeTypeHandler.class, "id");
		streamer.useAttributeFor(MimeTypeHandler.class, "uri");
		streamer.useAttributeFor(MimeTypeHandler.class, "mimeType");
		streamer.useAttributeFor(MimeTypeHandler.class, "active");
		streamer.aliasAttribute(MimeTypeHandler.class, "mimeType", "mime-type");
		// streamer.omitField(MimeTypeHandler.class, "handler");

	}

	public static void configureStreamerModule(XStream streamer) {
		streamer.alias("module", Module.class);
		streamer.aliasField("relative-uri", Module.class, "relativeUri");
		streamer.useAttributeFor(Module.class, "id");
		streamer.useAttributeFor(Module.class, "active");
		streamer.useAttributeFor(Module.class, "uri");
		streamer.useAttributeFor(Module.class, "name");
		streamer.omitField(Module.class, "resources");

	}

	public static void configureStreamerParameter(XStream streamer) {
		streamer.alias("parameter", Parameter.class);
		streamer.useAttributeFor(Parameter.class, "id");
		streamer.useAttributeFor(Parameter.class, "active");
		streamer.useAttributeFor(Parameter.class, "uri");
		streamer.useAttributeFor(Parameter.class, "name");
		streamer.useAttributeFor(Parameter.class, "value");

	}

	public static void configureStreamerResource(XStream streamer) {
		streamer.alias("resource", Resource.class);
		streamer.aliasField("target-uri", Resource.class, "targetUri");
		streamer.useAttributeFor(Resource.class, "id");
		streamer.useAttributeFor(Resource.class, "active");
		streamer.useAttributeFor(Resource.class, "uri");
		// streamer.omitField(Resource.class, "handler");
		streamer.omitField(Resource.class, "http-methods");

	}

}
